/*******************************************************************************
* This file declares all the public global variables.
*
* Author: Kong Wai Weng @ Cytron Technologies Sdn. Bhd.
*******************************************************************************/



#ifndef _VARIABLES_H
#define _VARIABLES_H



#include "FreeRTOS.h"
#include "semphr.h"
#include "flash.h"



/*******************************************************************************
* STRUCTURE, UNION AND ENUMERATION DEFINITION                                  *
*******************************************************************************/

// System state.
struct SYSTEM_STATE {
	// Indicate if we want to skip the error LED blinking.
	// 0 = LED indicates the system error.
	// 1 = LED is used for other information.
	unsigned int bSkipErrorLed;
	
	// Indicate whether the DCM is ready.
	// 0 = DCM is initializing.
	// 1 = DCM is ready.
	unsigned int bDcmReady;
	
	// Indicate whether the altitude sensor is ready.
	// 0 = The altitude sensor is initializing.
	// 1 = The altitude sensor is ready.
	unsigned int bAltitudeReady;
	
	// Indicate whether the airspeed sensor is ready.
	// 0 = The airspeed sensor is initializing.
	// 1 = The airspeed sensor is ready.
	unsigned int bAirspeedReady;
	
	// Indicate whether the control loop is ready.
	// 0 = The control loop is initializing.
	// 1 = The control loop is ready.
	unsigned int bControlReady;
	
	// Indicate whether the navigation loop is ready.
	// 0 = The navigation loop is initializing.
	// 1 = The navigation loop is ready.
	unsigned int bNavigationReady;
	
	// Indicate whether data logging is running.
	// 0 = Not running.
	// 1 = Running.
	unsigned int bLogRunning;
};



// System error flag.
// 0 = Good, 1 = Error.
struct SYSTEM_ERROR {
	unsigned int bAccError;			// Cannot communicate with the accelerometer.
	unsigned int bGyroError;		// Cannot communicate with the gyro.
	unsigned int bMagError;			// Cannot communicate with the magnetometer.
	unsigned int bPressureError;	// Cannot communicate with the pressure sensor.
	unsigned int bGpsError;			// No data received from the GPS.
	unsigned int bRcRxError;		// No signal received from the RC receiver.
	unsigned int bLogError;			// Not enough space or too many log records.
	
	unsigned int bRtosStackError;	// RTOS stack overflow or the watermark is too low.
};



// RTOS Stack High Water Mark.
// Used to indicate how much space left in the RTOS Stack for each task.
struct STACK_WATER_MARK {
	unsigned int uiDcmLoop;
	unsigned int uiBmp085Loop;
	unsigned int uiAirspeedLoop;
	unsigned int uiControlLoop;
	unsigned int uiLedError;
	unsigned int uiRcRxWatchdog;
	unsigned int uiGpsWatchdog;
	unsigned int uiMavlinkHeartbeat;
	unsigned int uiSLcd;
	unsigned int uiDataLog;
	unsigned int uiCml;
	unsigned int uiNavigationLoop;
};



/*******************************************************************************
* PUBLIC GLOBAL VARAIBLES PROTOTYPES                                           *
*******************************************************************************/

// System congiguration.
extern volatile struct SYSTEM_CONFIG xSystemConfig;

// System state.
extern volatile struct SYSTEM_STATE xSystemState;

// System error.
extern volatile struct SYSTEM_ERROR xSystemError;

// Stack watermark.
extern volatile struct STACK_WATER_MARK xStackWaterMark;



// Mutex to indicate that the I2C is being access.
extern volatile xSemaphoreHandle mutexI2cBusy1;
extern volatile xSemaphoreHandle mutexI2cBusy2;

// Mutex to indicate that the serial flash is being access.
extern volatile xSemaphoreHandle mutexSerialFlashBusy;



/*******************************************************************************
* PUBLIC FUNCTION PROTOTYPES                                                   *
*******************************************************************************/

/*******************************************************************************
* PUBLIC FUNCTION: vInitVar
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the public global variables.
*
*******************************************************************************/
void vInitVar(void);



#endif
